<?php

namespace Modules\Wechat\Service;

use Duxravel\Core\Exceptions\ErrorException;
use EasyWeChat;
use Modules\Wechat\Model\Setting;

class WechatService
{
//    $officialAccount = \EasyWeChat::officialAccount(); // 公众号
//    $work = \EasyWeChat::work(); // 企业微信
//    $payment = \EasyWeChat::payment(); // 微信支付
//    $openPlatform = \EasyWeChat::openPlatform(); // 开放平台
//    $miniProgram = \EasyWeChat::miniProgram(); // 小程序
    public function __construct(){

    }

    /**
     * 获取配置信息
     * @param string $type all|official_account|mini_app|payment|platform|work
     * @param int $id
     * @return array
     * @throws ErrorException
     */
    protected function config(string $type = 'all',int $id = 1)
    {
        $setting = $id?Setting::find($id):Setting::query()->limit(1)->first();
        if(!$setting){
            app_error("暂无配置信息");
        }
        $config = [
            'official_account'=>$setting->official_account,
            'mini_app'=>$setting->mini_app,
            'payment'=>$setting->payment,
            'platform'=>$setting->platform,
            'work'=>$setting->work
        ];
        return $type==='all'?$config:$config[$type];
    }

    /**
     * @param array $config
     * @return EasyWeChat\OfficialAccount\Application
     */
    protected function officialAccount(array $config = [])
    {
        return EasyWeChat::officialAccount($config);
    }
}
